<?php
/**
 * PHP Daycare
 * This file will handle checking a child in, including all checking
 * @author Jason Butz
 * @version 0.0
 * @package PHPDaycare
 */
include_once("functions.php");
include_once("config.php");
// The user MUST be logged in to view this page!
session_start();
if(!$session->isloggedin())
{
	echo "<p>You must be logged in to view this page. Please <a href=\"index.php\">login</a> to continue.</p>";
}
else // They are logged in!
{
	if(!isset($_GET['cid']))
	{
		// We need to get a list of children from the database
		$r = $db->query("SELECT ID, name FROM `child` WHERE (ID) NOT IN (SELECT attendance.child_id As `ID` FROM attendance WHERE attendance.out IS NULL);");
		echo "
		<p>Please select the child you wish to check in</p>
		<table cellspacing='1' class='tablesorter' width='100%'>
		<thead>
			<tr>
				<th class=\"{sorter: 'text'}\">Name</th>
				<th class=\"{sorter: 'text'}\">Options</th>
			</tr>
		</thead>
		<tbody>";
		while($row = mysql_fetch_row($r))
		{
			echo "<tr>
						<td>" . $row[1] . "</td>
						<td><a href= \"#\" onClick=\"loadinfo('checkin.php', 'cid=".$row[0]."')\">Check-In</a></td>
					</tr>";
		}
		echo "</tbody></table>";
		$temp->tablePager();
		echo "<script type=\"text/javascript\">loadsorter()</script>";
	}
	else if(isset($_GET['checkin']))
	{
		// We need to check-in this child
		// Do we have have all the info?
		$allSet = true;
		if(!isset($_GET['notes']))
		{
			echo "<p>Error: No Special Notes were set! This could be a hacking attempt.</p>";
			$allSet = false;
		}
		if(!isset($_GET['class']))
		{
			echo "<p>Error: No class was set! This could be a hacking attempt.</p>";
			$allSet = false;
		}
		if(!isset($_GET['adult']))
		{
			echo "<p>Error: No adult was set! This could be a hacking attempt.</p>";
			$allSet = false;
		}
		if(!isset($_GET['cid']))
		{
			echo "<p>Error: No child id was set! This could be a hacking attempt.</p>";
			$allSet = false;
		}
		if($allSet)
		{
			$notes = mysql_real_escape_string($_GET['notes']);
			//$notes = str_replace("\r",'<br>',$notes);
			// Step 1: Update the Special Notes
			$r1 = $db->query("UPDATE child SET notes = '".$notes."' WHERE ID = ".$_GET['cid'].";");
			if(!$r1) { echo "<p>An error occurred, this operation could not be completed.</p>"; }
			else
			{
			// Step 2: Check the child in
				// This query gets the ` in some places so MySQL isn't confused
				$r2 = $db->query("INSERT INTO attendance (`child_id`, `class_id`, `parent_in`, `in`) VALUES ('".$_GET['cid']."', '".$_GET['class']."', '".$_GET['adult']."', CURRENT_TIMESTAMP);");
				if(!$r2) {echo "<p>An error occurred, only the special notes were updated.</p>";}
				else
				{
					$log->checkin($_GET['cid'], $_GET['adult'], $_SESSION['user']);
					echo "<p>The operation completed successfully. The child was checked in.</p>";
				}
			}
		}
	}
	else
	{
		$cid = $_GET['cid'];
		// A child has been selected. We must verify the information
		// Does this child exist?
		$r = $db->query("SELECT ID, name FROM child WHERE ID=\"$cid\";");
		if(!mysql_fetch_row($r))
		{
			// No such child exists!
			echo "<p>Error: No such child exists!</p>";
		}
		else
		{
			// The child exists, we now need a list of classes
			// And we need to know who is checking them in
			$class = $db->query("SELECT ID, name FROM class;");
			$child = $db->query("SELECT name, notes FROM child WHERE ID=$cid;");
			$parent = $db->query("SELECT ID, name FROM `c_a_rel` JOIN `adults` ON c_a_rel.adults = adults.ID WHERE child = $cid");
			//Process DB stuff
			while($row = mysql_fetch_row($class))
			{
				$classes .= "<option value='".$row[0]."'>".$row[1]."</option>\n";
			}
			while($row = mysql_fetch_row($parent))
			{
				$adults .= "<label><input type='radio' name='adult' value='".$row[0]."' onClick='AdultVal=".$row[0]."' /><a class='button' href='#' onClick=\"placePhoto(".$row[0].", 'a');placeTitle('".$row[1]."');\">".$row[1]."</a></label><br />";
			}
			$child = mysql_fetch_row($child);
			//
			//Output HTML
			echo "<h2>Currently checking in: ".$child[0]." </h2>
				<form name=\"checkin\" method='post' action=''>
				  <p>
					 <label>Class:
						<select name='class' id='class' tabindex='2'>
						  ".$classes."
						</select>
					 </label>
				  </p>
				  <p>
					 ".$adults."
				  </p>
				  <p>
					 <label>Special Notes<br />
						<textarea name='notes' id='notes' cols='45' rows='5' tabindex='3' onChange=\"outputtxt=newlinefix(document.checkin.notes.value)\">".br2nl($child[1])."</textarea>
						<script>var outputtxt = newlinefix(document.checkin.notes.value);</script>
					 </label>
				  </p>
				  <p>
					 <!-- <input type='submit' name='checkin' id='checkin'  tabindex='4' value='Complete Checkin' /> -->
					 <input type='button' tabindex='4' value='Complete Checkin' onClick=\"loadinfo('checkin.php', 'cid=".$cid."&checkin=1&class='+document.checkin.class.value+'&adult='+AdultVal+'&notes='+outputtxt)\"/>
				  </p>
				</form>";
			$temp->jqPopup("", "");
			echo "<script type='text/javascript'>loadPopupCalls()</script>";
		}
	}
}
